/*
用法

gulp 一次性编译
gulp watch 动态编译
gulp auto 动态编译并自动刷新

*/


var path = require('path');
var gulp = require('gulp');
var less = require('gulp-less');
var uglify = require('gulp-uglify');
var concat = require('gulp-concat');
var order = require('gulp-order');
var amdOptimize = require("amd-optimize");
var gulpif = require('gulp-if');
var cleanCss = require('gulp-clean-css');
var del = require('del');
var seq = require('gulp-sequence');
var print = require('gulp-print');

var pump = require('pump');

var browserSync = require('browser-sync')
var reload = browserSync.reload


//压缩选项
var con = false;

//构建开始前，用来清除目标位置下的文件
gulp.task('clean', function() {
    return del(['js/dist', 'css']);
})


gulp.task('style', function() {
    return gulp.src([
        './less/style.less', './less/assets/*'
    ])
    .pipe(less())
    .pipe(gulpif(con, cleanCss()))
    .pipe(gulp.dest('./css'))
});

//assets
gulp.task('assets', function () {
  return gulp.src('js/src/assets/**')
  .pipe(gulpif(con, uglify()))
  .pipe(gulp.dest('./js/dist/assets/'));
})

// 基础库
gulp.task('libjs', function(cb) {

  pump([
      gulp.src('js/src/lib/**'),
      order([
        // 'es5-shim.js',
          'jquery.js',
          'translate.js',
          'translate/**.js',
          'jquery.extensions.js',
          'i18next.min.js',
          'jquery-i18next.min.js',
          'i18n/**.js',
          'i18next.init.js',
          'require.js',
          '*.js'
        // 'jquery.cookie.js',
        // 'jquery.mousewheel.js',
      ]),
      concat('lib.js'),
      gulpif(con, uglify()),
      gulp.dest('./js/dist/base/')
    ],
    cb
  );

    /*return gulp.src('js/src/lib/**')
    .pipe(order([
        // 'es5-shim.js',
    	'jquery.js',
        'require.js',
        'translate.js',
        'translate/**.js',
        'jquery.extensions.js',
        '*.js'
    	// 'jquery.cookie.js',
    	// 'jquery.mousewheel.js',
    ]))
    .pipe(print())
    .pipe(concat('lib.js'))
    .pipe(gulpif(con, uglify()))
    .pipe(gulp.dest('./js/dist/base/'))*/
});

//组件
gulp.task('modulejs', function (cb) {

  pump([
      gulp.src('js/**.js'),
      amdOptimize('init', {
        name: 'init',
        baseUrl: './js',
        configFile: './js/init.js'
      }),
      concat('module.js'),
      gulpif(con, uglify()),
      gulp.dest('./js/dist/base/')
    ],
    cb
  )

    /*return gulp.src('js/**.js')
    .pipe(amdOptimize('init', {
        name: 'init',
        baseUrl: './js',
        configFile: './js/init.js'
    }))
    .pipe(concat('module.js'))
    .pipe(gulpif(con, uglify({
        // mangle: true
    })))
    .pipe(gulp.dest('./js/dist/base/'));*/
});

//公共代码 = 基础库 + 通用组件
gulp.task('basejs', ['libjs', 'modulejs'], function (cb) {

  pump([
      gulp.src('js/dist/base/**.js'),
      order([
        'lib.js',
        'module.js',
        '*.js'
      ]),
      concat('base.js'),
      gulpif(con, uglify()),
      gulp.dest('./js/dist/')
    ],
    cb
  )

    // return gulp.src('js/dist/base/**.js')
    // .pipe(order([
    //     'lib.js',
    //     'module.js',
    //     '*.js'
    // ]))
    // .pipe(concat('base.js'))
    // .pipe(gulpif(con, uglify()))
    // .pipe(gulp.dest('./js/dist/'))
})

// 页面代码
gulp.task('widgetjs', function (cb) {

  pump([
      gulp.src('js/src/widgets/**'),
      gulpif(con, uglify()),
      gulp.dest('./js/dist/widgets/')
    ],
    cb
  )

    /*return gulp.src('js/src/widgets/**')
    .pipe(gulpif(con, uglify()))
    .pipe(gulp.dest('./js/dist/widgets/'))*/
})


//监听修改
gulp.task('watch', ['default'], function() {

    gulp.watch('less/**', ['style']);

    gulp.watch('js/src/lib/**', ['libjs']);
    gulp.watch('{js/src/modules/**,js/init.js}', ['modulejs']);
    gulp.watch('js/dist/base/**', ['basejs']);

    gulp.watch('js/src/widgets/**', ['widgetjs'])

})

//监听修改并自动刷新
gulp.task('auto', ['default'], function() {

    gulp.watch('less/**', ['style']);

    gulp.watch('js/src/lib/**', ['libjs']);
    gulp.watch('{js/src/modules/**,js/init.js}', ['modulejs']);
    gulp.watch('js/dist/base/**', ['basejs']);

    gulp.watch('js/src/widgets/**', ['widgetjs'])

    browserSync({
        server: {
            baseDir: './'
        }
    })

    gulp.watch(['*.html', 'css/**', 'js/dist/**', 'html/**'], {cwd: './'}, reload);
})

// seq方法用来控制task的执行顺序
gulp.task('default', seq('clean', ['style', 'basejs', 'widgetjs']));
